package com.example.app.controller;

import com.example.app.config.AuthConfig;
import com.example.app.entity.ReqData;
import com.example.app.service.AuthService;
import com.example.app.util.HttpClient;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private AuthConfig authConfig;
    @Autowired
    private AuthService authService;

    /**
     * 授权登录
     *
     * @return
     */
    @GetMapping("login")
    public Object getLogin() {
        Map<String, String> map = new HashMap<>();
        /**
         * 获取签名对象
         */
        ReqData sign = authService.getSign();
        /**
         * 请求接口获取token
         */
        String postClient = HttpClient.postClient(authConfig.getAuthRest() + "token", JSONObject.fromObject(sign).toString());
        JSONObject jsonObject = JSONObject.fromObject(postClient);
        map.put("token", jsonObject.getJSONObject("rsp").getString("token"));
        map.put("redirectUrl", authConfig.getRedirectUrl());
        map.put("responseType", authConfig.getResponseType());
        String params = HttpClient.appendUrl(authConfig.getAuthUrl(), map);
        String url = "redirect:" + params;
        return url;
    }


}
